﻿using System;
using System.Collections;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using NanoERP.Common;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace NanoERP.BaseInfo
{
    //[Module("FrmSelectAccSubject")]
    public partial class FrmSelectAccSubject : ModuleForm
    {
        readonly AccountsubjectBLL _accountsubjectBLL;
        public FrmSelectAccSubject()
        {
            InitializeComponent();

            _accountsubjectBLL = new AccountsubjectBLL();

            QueryInfo();

            treeList.AfterExpand += (sender, e) => treeListColumn1.BestFit();
            treeList.DoubleClick += treeList_DoubleClick;
            txtKey.TextChanged += txtKey_TextChanged;
        }

        void QueryInfo()
        {
            treeList.Nodes.Clear();
            treeList.BeginUnboundLoad();
            var list = _accountsubjectBLL.Query(0);
            list.ForEach(r =>
            {
                var tgn = treeList.AppendNode(new object[] { r.Subjectno, r.Subjectname, r.Subjectpym, r.Beginmoney, r.Endmoney }, null);
                tgn.Tag = r;
                var listSec = _accountsubjectBLL.Query(r.Id);
                RecursiveLoopNodes(listSec, tgn);
            });

            treeList.EndUnboundLoad();
        }

        /// <summary>
        /// 递归循环调用
        /// </summary>
        /// <param name="firList">一级树集合</param>
        /// <param name="pNode">上级节点</param>
        void RecursiveLoopNodes(List<Accountsubject> firList, TreeListNode pNode)
        {
            firList.ForEach(r =>
            {
                var thiNode = treeList.AppendNode(new object[] { r.Subjectno, r.Subjectname, r.Subjectpym, r.Beginmoney, r.Endmoney }, pNode);
                thiNode.Tag = r;
                var listSec = _accountsubjectBLL.Query(r.Id);
                if (listSec.Count > 0)
                    RecursiveLoopNodes(listSec, thiNode);
            });
        }

        void treeList_DoubleClick(object sender, System.EventArgs e)
        {
            var node = treeList.FocusedNode;
            if (node == null) return;

            var obj = (Accountsubject)node.Tag;
            if (node.Nodes.Count > 0)
            {
                MessageBox.Show(@"当前科目包含子科目,不能选择.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.Tag = obj;
            this.DialogResult = DialogResult.OK;
        }

        private IEnumerable<TreeListNode> _treeNodes;
        private int _nodeIndex;
        private void btnQuery_Click(object sender, System.EventArgs e)
        {
            if (_treeNodes == null || !_treeNodes.Any()) return;
            _treeNodes.ToArray()[_nodeIndex].Selected = true;
            _nodeIndex = (_nodeIndex < _treeNodes.Count() - 1) ? ++_nodeIndex : 0;
        }

        void txtKey_TextChanged(object sender, EventArgs e)
        {
            _nodeIndex = 0;
            if (_nodeIndex == 0)
                _treeNodes = treeList.Nodes.TreeWhere(t =>
                {
                    var acc = t.Tag as Accountsubject;
                    var regex = string.Format("{0}|{1}{2}", acc.Subjectno, acc.Subjectname, acc.Subjectpym);
                    return Regex.IsMatch(regex, @"(?i)" + txtKey.Text);
                });
        }
    }
}
